<?php
namespace app\admin\controller;
use app\admin\controller\BaseAdmin;
use function Couchbase\defaultDecoder;
use GatewayWorker\Lib\Gateway;
use think\Db;
use think\Request;
use app\admin\model\Company as CompanyModel;
use app\admin\model\Position as PositionModel;
class Feedbacktag extends BaseAdmin
{

    /**
     * 列表
     * @return mixed
     */
    public function index(Request $request){
        if (request()->isAjax()){
            return \app\admin\model\Feedbacktag::getlist($request);
        }else{
//            $company = '';
//            if ($company_id = $request->get('company_id')) {
//                $company = \app\admin\model\Company::find($company_id)->toArray();
//            }

//            $this->assign('company', $company);
            $pageno = input('param.pageno', 1);
            $this->assign('pageno', $pageno);
            return $this->fetch();
        }
    }

    public function set()
    {
        if (request()->isAjax()){
            if(request()->isPost()){
                $post = request()->post();

                if(!$post['content']){
                    return ['status'=>false,'message'=>'内容不能为空'];
                }



                $id = !empty($post['id'])?$post['id']:0;
                if($id){
                    unset($post['id']);
                    $res = Db::name('feedbacktag')->where('id', $id)->update($post);
                    if($res!==false){
                        return ['status'=>true, 'message'=>'更新成功'];
                    }else{
                        return ['status'=>false,'message'=>'更新失败'];
                    }
                }else{
                    $res = Db::name('feedbacktag')->insertGetId($post);
                    if($res){
                        return ['status'=>true, 'message'=>'新增成功'];
                    }else{
                        return ['status'=>false,'message'=>'新增失败'];
                    }
                }
            }else{
                $id = request()->get('id');
                $user = Db::name('feedbacktag')->where('id', $id)->find();
                return $user;
            }
        }else{
            $pageno = request()->get('pageno')?request()->get('pageno'):1;
            $this->assign('id',request()->get('id'));
            $this->assign('pageno', $pageno);
            return $this->fetch();
        }
    }

    /**
     * 删除
     * @return array
     */
    public function remove(){
        $ids = request()->post('ids');
        if(request()->isAjax()){
            $ids = json_decode($ids, true);
            $result = Db::name('feedbacktag')->whereIn('id', $ids)->delete();
            if($result){
                return returnData();
            }else{
                return ['status'=>false,'message'=>'删除失败!'];
            }
        }else{
            new \HttpException(502,'请求不合法');
        }
    }

}
